#include"LinkQueue.h"
#include"LinkStack.h"
#include"MyString.h"
#include<stdio.h>
#include<stdlib.h>

#define MaxNum 3

struct Car
{
    MyString name;
    double arriveTime;
    double leaveTime;
};

struct Car *CreateCar(const char *name,double arriveTime)
{
    struct Car *newCar=(struct Car*)malloc(sizeof(struct Car));
    if(newCar==NULL)
    {
        printf("newCar malloc erroe!\n");
        return NULL;
    }
    StringInitialize(&newCar->name,name);
    newCar->arriveTime=arriveTime;
    newCar->leaveTime=0;
}

void FreeCar(struct Car *leaveTime)
{
    if(leaveTime==NULL)
        return;
    FreeString(&leaveTime->name);
    free(leaveTime);
}

void Come_Car(LStack *CarStop,LQueue *pavement)
{
    char CarName[100]={0};
    double arriveTime=0;
    printf("请输入你的车牌");
    scanf("%s",CarName);
    printf("请输入你的到达时间:");
    scanf("%lf",&arriveTime);

    struct Car *newCar=CreateCar(CarName,arriveTime);
    if(newCar==NULL)
    {
        printf("newCar malloc Error!\n");
        return;
    }
    if(CarStop->stack.len>=MaxNum)
    {
        QPush(pavement,newCar);
        printf("停车场已满,汽车|%s|停在了便道第%d个位置.\n",newCar->name.string,pavement->queue.len);
    }
    else
    {
        Push(CarStop,newCar);
        printf("停车场未满,汽车|%s|停在了停车场第%d个位置.\n",newCar->name.string,CarStop->stack.len);
    }
}

void Leave_Car(LStack *CarStop,LQueue *pavement)
{
    printf("请输入要离开车辆的车牌:");
    char temp[100]={0};
    scanf("%s",temp);
    Init_MyString(CarName,temp);

    LStack *TempStop;
    InitLinkStack(&TempStop);
    int Num=CarStop->stack.len;
    double LeaveCarTime=0;

    while(IsEmpty(CarStop)==false)
    {
        struct Car *car=(struct Car*)*Pop(CarStop);
        if(car->name.isEqual(&car->name,&CarName)==true)
        {
            printf("请输入车辆的离开时间:");
            scanf("%lf",&car->leaveTime);
            printf("汽车|%s|在%lf时间到达停车场,在%lf时间离开停车场,停车费用为%lf\n",&car->name.string,&car->arriveTime,&car->leaveTime,(&car->leaveTime-&car->arriveTime)*5);
            FreeCar(car);
            break;
        }
        else
        {
            Push(&TempStop,car);
        }
    }
    while(IsEmpty(&TempStop)==false)
    {
        Push(CarStop,*Pop(&TempStop));
    }
    if(Num==CarStop->stack.len)
    {
        printf("未找到你要的车牌!\n");
    }
    else
    {
        if(IsQEmpty(pavement)==false)
        {
            struct Car *car=(struct Car*)*QPop(pavement);
            car->arriveTime=LeaveCarTime;
            Push(CarStop,car);
            printf("汽车|%s|在%lf时间停在了停车场第%d个位置.\n",car->name.string,car->arriveTime,pavement->queue.len);
        }
    }
}

int main()
{
    LStack CarStop;
    InitLinkStack(&CarStop);
    LQueue pavement;
    InitLQueue(&pavement);

    int choice=0;
    while(1)
    {
        printf("**********************CarStop***********************\n");
        printf("1.车辆进入停车场\n");
        printf("2.车辆离开停车场\n");
        printf("3.查询停车场状态\n");
        printf("4.查询便道状态  \n");
        printf("5.退出停车场系统\n");

        printf("请输入你的选择:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                Come_Car(&CarStop,&pavement);
                break;
            default:
                break;
        }



    }
    return 0;
}